package com.gaga.console.base.utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.gaga.console.base.controller.BaseController;
import com.gaga.console.base.entity.BaseEntity;
import com.gaga.console.base.entity.GenCode;
import com.gaga.console.base.mapper.SuperMapper;
import com.gaga.console.base.service.ISuperService;
import com.gaga.console.base.service.impl.SuperServiceImpl;

import java.util.Collections;
import java.util.ResourceBundle;

/**
 * <p>
 * 代码生成器
 * </p>
 */
public class MybatisPlusGenerator {

    public static void genCode(GenCode genCode) {
        if(genCode == null){
            // 获取mybatis-plus-generator.properties文件的配置信息
            final ResourceBundle rb = ResourceBundle.getBundle("mybatis-plus-generator");
            genCode = new GenCode()
                    .setAuthor(rb.getString("author")).setParent(rb.getString("parent"))
                    .setUrl(rb.getString("url")).setUserName(rb.getString("userName"))
                    .setPassword(rb.getString("password")).setOutputDir(rb.getString("OutputDir"))
                    .setOutputDirXml(rb.getString("OutputDirXml")).setTableName(rb.getString("tableName"));
        }
        String projectPath = System.getProperty("user.dir");

        GenCode finalGenCode = genCode;
        FastAutoGenerator.create(finalGenCode.getUrl(), finalGenCode.getUserName(), finalGenCode.getPassword())
                .globalConfig(builder -> {
                    builder.author(finalGenCode.getAuthor())// 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .outputDir(projectPath + finalGenCode.getOutputDir()); // 指定输出目录

                })
                .packageConfig(builder -> {
                    builder.parent(finalGenCode.getParent()) // 设置父包名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + finalGenCode.getOutputDirXml()));
                })
                .injectionConfig(builder -> {
                    builder.customFile(Collections.singletonMap("api.js", "/templates/api.js.ftl"));
                })
                //实体策略配置
                .strategyConfig(builder -> {
                    builder.entityBuilder()
                            .superClass(BaseEntity.class)
                            //开启Lombok，默认生成@Get，@Set，可以手动换成@Data
                            .enableChainModel()
                            .enableLombok()
                            .addSuperEntityColumns("remark", "enabled", "seqNo", "delFlag", "createBy", "createTime", "updateBy", "updateTime", "deleteBy", "deleteTime");
                })
                //控制类策略配置
                .strategyConfig(builder -> {
                    builder.controllerBuilder()
                            //如果没有父类请注释掉
                            .superClass(BaseController.class)
                            .enableRestStyle();
                })
                //服务类策略配置
                .strategyConfig(builder -> {
                    builder.serviceBuilder()
                            .superServiceClass(ISuperService.class)
                            .superServiceImplClass(SuperServiceImpl.class)
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl");
                })
                //mapper策略配置
                .strategyConfig(builder -> {
                    builder.mapperBuilder()
                            .superClass(SuperMapper.class)
                            .enableBaseResultMap()
                            .enableBaseColumnList();
                })
                .strategyConfig(builder -> {
                    builder.addInclude(finalGenCode.getTableName().split(","));
                })
                .templateConfig(builder -> {
                    builder.disable(TemplateType.ENTITY, TemplateType.CONTROLLER)
                            .entity("/templates/entity.java")
                            .controller("/templates/controller.java");
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();

    }


    public static void main(String[] args) {

        genCode(null);

        System.out.println("done,fresh engineering");
    }

}




